package com.dllo.lanou.cookapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dllo.lanou.cookapp.R;
import com.dllo.lanou.cookapp.entity.AddCookEntity;
import com.dllo.lanou.cookapp.volly.GetVolleyImage;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by xijinhua on 2016/3/24.
 */
public class AddCookStrpAdapter extends BaseAdapter implements View.OnClickListener {
   private List<AddCookEntity>  addCookEntities ;
    private Context context;
    private LayoutInflater mInflater;
    private ValueListener valueListener;
private AddCookEntity   addCookEntity;
    public void setValueListener(ValueListener valueListener){
        if(valueListener == null){

        }
        this.valueListener = valueListener;
    }
    public AddCookStrpAdapter(Context context){
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }
    public void addData(AddCookEntity   addCookEntity){
       if (addCookEntities == null){
            addCookEntities = new ArrayList<>();
     }
        addCookEntities.add(addCookEntity);
        notifyDataSetChanged();
    }
    public void addData(List<AddCookEntity> addCookEntity){

        this.addCookEntities = addCookEntity;
        Log.i("AddCookEntity",addCookEntities.size()+"");
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return addCookEntities !=null &&addCookEntities.size()>0 ?addCookEntities.size():0 ;
    }

    @Override
    public Object getItem(int position) {
        return addCookEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView =mInflater.inflate(R.layout.item_addstep,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }

        AddCookEntity addCookEntity = addCookEntities.get(position);
        Log.i("AddCookEntity",addCookEntities.size()+"");
        holder.textView.setText(addCookEntity.getCount());
        //给图片传值
        holder.imageView.setOnClickListener(this);
        holder.imageView.setTag(position);
        holder.content.setOnClickListener(this);
        holder.content.setTag(position);
        String path = addCookEntities.get(position).getToYunImg();
   //     Log.i("AddCookEntity",path);
        if (path!=null){
        if (path.contains("http://")){
            GetVolleyImage.getNetWorkImg(path,holder.imageView);
        }}
        else{
            String path2 = addCookEntities.get(position).getImg();

            if (path2 != null){
                File file = new File(path);
                if(file.exists()){

                    Bitmap bm = BitmapFactory.decodeFile(path);
                    holder.imageView.setImageBitmap(bm);
                }
            }else {
                holder.imageView.setImageResource(R.drawable.image_button_add);
            }
        }


        String content = addCookEntities.get(position).getContent();
        if (content != null){
            holder.content.setText(content);
        }
        else
        {
            holder.content.setText("添加内容");
        }





        return convertView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_img:
                int postion = (int) v.getTag();
                valueListener.onClick(postion);
                break;

            case R.id.add_content:
                int postion2 = (int) v.getTag();
                valueListener.onContentClick(postion2);
        }
    }

    static class ViewHolder{

        @Bind(R.id.add_count)
        TextView textView ;
        @Bind(R.id.add_content)
        TextView content;
        @Bind(R.id.add_img)
        ImageView imageView;

        public ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
    }
public interface  ValueListener{
    void onClick(int postion);
    void onContentClick(int postion);
}


}
